package com.liushuang.waiwaiim.controller.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.hyphenate.easeui.modules.contact.EaseContactListFragment;
import com.hyphenate.easeui.widget.EaseSearchTextView;
import com.liushuang.waiwaiim.R;
import com.liushuang.waiwaiim.controller.activity.AddContactActivity;
import com.liushuang.waiwaiim.controller.activity.InviteActivity;
import com.liushuang.waiwaiim.utils.Constant;
import com.liushuang.waiwaiim.utils.SpUtils;

public class ContactListFragment extends EaseContactListFragment {

    private EaseSearchTextView tvSearch;
    private ImageButton mIbtAdd;
    private ImageView mImageRed;
    private LocalBroadcastManager mLBM;
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            //当有邀请信息发送过来时，更新红点显示
            mImageRed.setVisibility(View.VISIBLE);
//            Toast.makeText(context, "受到来自别人的邀请", Toast.LENGTH_SHORT).show();

            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);
        }
    };
    private LinearLayout mLlContactInvite;

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        addSearchView();
        //设置无数据时空白页面
        contactLayout.getContactList().getListAdapter().setEmptyLayoutResource(R.layout.demo_layout_friends_empty_list);
        addHeader();

    }

    private void addSearchView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.demo_layout_search, null);
        llRoot.addView(view, 0);
        tvSearch = view.findViewById(R.id.tv_search);
        mIbtAdd = view.findViewById(R.id.id_ibt_add);
        tvSearch.setHint(R.string.em_friend_list_search_hint);
    }

    private void addHeader() {
        /*contactLayout.getContactList().addCustomItem(R.id.contact_header_item_new_chat, R.drawable.em_friends_new_chat, getString(R.string.em_friends_new_chat));
        contactLayout.getContactList().addCustomItem(R.id.contact_header_item_group_list, R.drawable.em_friends_group_chat, getString(R.string.em_friends_group_chat));*/

        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_fragment_contact, null);
        contactLayout.getContactList().addHeaderView(headerView);

        //获取红点对象
        mImageRed = headerView.findViewById(R.id.iv_contact_red);

        mLlContactInvite = headerView.findViewById(R.id.ll_contact_invite);

        mLBM = LocalBroadcastManager.getInstance(getActivity());
        mLBM.registerReceiver(mBroadcastReceiver, new IntentFilter(Constant.CONTACT_INVITE_CHANGED));


    }

    @Override
    public void initListener() {
        super.initListener();

        mIbtAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddContactActivity.class);
                startActivity(intent);
            }
        });

        //初始化红点显示
        boolean isNewInvite = SpUtils.getInstance().getBoolean(SpUtils.IS_NEW_INVITE, false);
        mImageRed.setVisibility(isNewInvite ? View.VISIBLE : View.GONE);

        mLlContactInvite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //红点处理
                mImageRed.setVisibility(View.GONE);
                SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, false);

                //跳转到邀请信息列表页面
                Intent intent = new Intent(getActivity(), InviteActivity.class);
                startActivity(intent);

            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLBM.unregisterReceiver(mBroadcastReceiver);
    }
}